//
//  Ext+Buttom.swift
//  Swift微博
//
//  Created by 董筱泽 on 16/10/22.
//  Copyright © 2016年 董筱泽. All rights reserved.
//

import UIKit

extension UIButton{
    
    
    convenience init(setImageName: String, backGroundName:String,target: Any?, action: Selector) {
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        self.setImage(UIImage(named: setImageName), for: UIControlState.normal)
        self.setImage(UIImage(named: "\(setImageName)_highlighted"), for: UIControlState.highlighted)
        self.setBackgroundImage(UIImage(named: backGroundName), for: UIControlState.normal)
        self.setBackgroundImage(UIImage(named: "\(backGroundName)_highlighted"), for: UIControlState.highlighted)
        self.sizeToFit()
    }
    
    
    convenience init(setHighlightedImgName: String? = nil, title:  String? = nil, target: Any?, action: Selector) {
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        if let img = setHighlightedImgName {
            // 设置 image
            self.setImage(UIImage(named: img), for: UIControlState.normal)
            self.setImage(UIImage(named: "\(img)_highlighted"), for: UIControlState.highlighted)
        }
        
        if let tit = title {
            self.setTitle(tit, for: UIControlState.normal)
            self.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
            self.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
            titleLabel?.font = UIFont.systemFont(ofSize: 14)
        }
        
        self.sizeToFit()
    }
    
    /// 实例化一个有背景图片和文字的 button
    ///
    /// - parameter setBackgroundImgName:
    /// - parameter title:                文字
    /// - parameter fontSize:             文字大小
    /// - parameter titleColor:           文字颜色
    /// - parameter target:
    /// - parameter action:
    ///
    /// - returns: <#return value description#>
    convenience init(setBackgroundImgName: String, title:  String, fontSize: CGFloat, titleColor: UIColor,  target: Any?, action: Selector) {
        self.init()
        // 添加点击事件
        self.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
        self.setBackgroundImage(UIImage(named: setBackgroundImgName), for: UIControlState.normal)
        self.setTitle(title, for: UIControlState.normal)
        self.setTitleColor(titleColor, for: UIControlState.normal)
        titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        self.sizeToFit()
    }


}
